# Step 1: 下载 Maven
$mavenVersion = "3.8.8" # 您可以根据需要更改 Maven 版本
$mavenUrl = "https://repo.huaweicloud.com/apache/maven/maven-3/3.8.8/binaries/apache-maven-3.8.8-bin.zip"

# 定义下载路径

$destinationPath = "$env:TEMP\apache-maven-$mavenVersion.zip"

# 下载 maven 
Write-Host "Downloading Maven..."
Invoke-WebRequest -Uri $mavenUrl -OutFile $destinationPath

# Step 2: 解压 Maven
$extractPath = "C:\Program Files\Apache\maven"
Expand-Archive -Path $destinationPath -DestinationPath $extractPath

# Step 3: 配置环境变量
$env:MAVEN_HOME = "$extractPath\apache-maven-$mavenVersion"
[System.Environment]::SetEnvironmentVariable("MAVEN_HOME", $env:MAVEN_HOME, [System.EnvironmentVariableTarget]::Machine)

$envPath = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine)
if ($envPath -notcontains "$env:MAVEN_HOME\bin") {
    $newEnvPath = "$envPath;$env:MAVEN_HOME\bin"
    [System.Environment]::SetEnvironmentVariable("Path", $newEnvPath, [System.EnvironmentVariableTarget]::Machine)
}


Write-Host "Maven installation and configuration completed."
